import { http } from '@/utils/request';

// 订单详情业务对象
export interface OrderDetailBo {
    id?: number;
    orderId?: number;
    productId: number;
    productName: string;
    productImage?: string;
    quantity: number;
    unitPrice: number;
    totalPrice: number;
}

// 订单优惠券
export interface OrderCoupon {
    id?: number;
    orderId?: number;
    couponId: number;
    discountAmount: number;
}

// 订单业务对象
export interface OrderBo {
    id?: number;
    orderNo: string;
    userId: number;
    deviceId: number;
    totalAmount: number;
    actualAmount: number;
    discountAmount?: number;
    payType: number; // 支付方式(1:微信,2:支付宝,3:余额)
    payTime?: string;
    status: number; // 订单状态(0:待支付,1:支付中,2:支付成功,3:支付失败,4:已取消,5:已完成)
    outTradeNo?: string;
    completeTime?: string;
    cancelTime?: string;
    cancelReason?: string;
    remark?: string;
    orderDetail?: OrderDetailBo[];
    orderCoupon?: OrderCoupon;
}

// 订单视图对象
export interface OrderVo {
    id: number;
    orderNo: string;
    userId: number;
    deviceId: number;
    totalAmount: number;
    actualAmount: number;
    discountAmount?: number;
    payType: number;
    payTime?: string;
    status: number;
    outTradeNo?: string;
    completeTime?: string;
    cancelTime?: string;
    cancelReason?: string;
    remark?: string;
    createTime?: string;
    updateTime?: string;
    orderDetail?: OrderDetailBo[];
    orderCoupon?: OrderCoupon;
}

// 创建订单
export const createOrder = (data: any) => {
    return http.post('/order/api/order', data);
};

// 创建订单优惠券
export const createOrderCoupon = (data: any) => {
    return http.post('/order/api/orderCoupon', data);
};

// 获取订单列表
export const getOrderList = (params?: any) => {
    return http.get('/order/api/order/list', params);
};

// 获取订单详情
export const getOrderDetail = (id: number) => {
    return http.get(`/order/api/order/${id}`);
};
// 获取订单详情
export const getByOrderNo = (orderNo: string) => {
    return http.get(`/order/api/order/getByOrderNo/${orderNo}`);
};

// 删除订单
export function deleteOrder(ids: number[]) {
    return http.delete('/order/api/order/' + ids.join(','));
}

// 更新订单状态
export const updateOrderStatus = (orderData: OrderVo, status: number) => {
    return http.put('/order/api/order/order', {
        ...orderData,
        status: status
    });
};

// 订单取消
export const orderCancel = (orderNo: string) => {
    return http.get('/order/api/order/orderCancel', { orderNo });
};